﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using iTextSharp.text;
using iTextSharp.text.pdf;
using System.IO;
using System.Drawing;
using Domain;
using BusinessTier.Facade.Implementation;
using BusinessTier.Facade.Interface;
using Simply.BusinessTier.FileStorage;

namespace Simply.BusinessTier.PDF
{


    internal class PDFService
    {
        

        /// <summary>
        /// 90x50mm standard size width with 2mm extension for 
        /// cutting
        /// 92mm in points
        /// </summary>
        public static readonly float NATIONAL_BC_WIDTH = 260.787f;

        /// <summary>
        /// 90x50mm standard size height with 2mm extension for 
        /// cutting
        /// 52mm in points
        /// </summary>
        public static readonly float NATIONAL_BC_HEIGHT = 147.401f;

        public static readonly float NATIONAL_BC_VIEW_HEIGHT = 361f;
        public static readonly float NATIONAL_BC_VIEW_WIDTH = 650f;

        public static readonly float A4_MARGIN_SIDES = 36.713f;
        public static readonly float A4_MARGIN_TOPBOT = 52.497f;

        /// <summary>
        /// Stroke line with used for registration marks
        /// 0.2mm in points
        /// </summary>
        public static readonly float REG_STROKE_WIDTH = 0.567f;
        public static readonly float CUT_EXT_LENGTH = 5.669f;


        /// <summary>
        /// Draws registration marks for cutting
        /// using pdf content byte object
        /// </summary>
        /// <param name="cb"></param>
        private void DrawRegistrationMarks(PdfContentByte cb)
        {
            cb.SetColorStroke(new PdfSpotColor("All", BaseColor.GRAY), 1f);
            cb.SetLineWidth(REG_STROKE_WIDTH);

            for (int i = 0; i < 5; i++)
            {
                float startX = A4_MARGIN_SIDES - CUT_EXT_LENGTH;
                float startY = A4_MARGIN_TOPBOT + i * NATIONAL_BC_HEIGHT + CUT_EXT_LENGTH / 2;

                cb.MoveTo(startX, startY);
                cb.LineTo(startX + CUT_EXT_LENGTH, startY);

                startX = PageSize.A4.Width - startX;

                cb.MoveTo(startX, startY);
                cb.LineTo(startX - CUT_EXT_LENGTH, startY);

                startY = A4_MARGIN_TOPBOT + (i + 1) * NATIONAL_BC_HEIGHT - CUT_EXT_LENGTH / 2;

                cb.MoveTo(startX, startY);
                cb.LineTo(startX - CUT_EXT_LENGTH, startY);


                startX = PageSize.A4.Width - startX;

                cb.MoveTo(startX, startY);
                cb.LineTo(startX + CUT_EXT_LENGTH, startY);
            }


            for (int i = 0; i < 2; i++)
            {
                float startX = A4_MARGIN_SIDES + i*NATIONAL_BC_WIDTH + CUT_EXT_LENGTH/2;
                float startY = A4_MARGIN_TOPBOT - CUT_EXT_LENGTH;

                cb.MoveTo(startX, startY);
                cb.LineTo(startX, startY + CUT_EXT_LENGTH);

                startY = PageSize.A4.Height - startY;

                cb.MoveTo(startX, startY);
                cb.LineTo(startX, startY - CUT_EXT_LENGTH);

                startX = A4_MARGIN_SIDES + (i + 1) * NATIONAL_BC_WIDTH - CUT_EXT_LENGTH / 2;

                cb.MoveTo(startX, startY);
                cb.LineTo(startX, startY - CUT_EXT_LENGTH);

                startY = PageSize.A4.Height - startY;

                cb.MoveTo(startX, startY);
                cb.LineTo(startX, startY + CUT_EXT_LENGTH);
            }

            float vlStartX = A4_MARGIN_SIDES - CUT_EXT_LENGTH - REG_STROKE_WIDTH/2;
            cb.MoveTo(vlStartX, 10);
            cb.LineTo(vlStartX, PageSize.A4.Height - 10);

            float hlStartY = PageSize.A4.Height - A4_MARGIN_TOPBOT + CUT_EXT_LENGTH + REG_STROKE_WIDTH / 2;
            cb.MoveTo(10, hlStartY);
            cb.LineTo(PageSize.A4.Width - 10, hlStartY);
            cb.Stroke();
        }

        private void DrawCutRectangle(PdfContentByte cb)
        {
            cb.SetCMYKColorStroke(100, 0, 0, 0);
            cb.SetLineWidth(REG_STROKE_WIDTH);

            cb.Rectangle(A4_MARGIN_SIDES + CUT_EXT_LENGTH / 2, A4_MARGIN_TOPBOT + CUT_EXT_LENGTH/2,
                NATIONAL_BC_WIDTH - CUT_EXT_LENGTH, NATIONAL_BC_HEIGHT - CUT_EXT_LENGTH);
        }

        
        public void GeneratePDF(InfoTemplate infoData, long productId, string absolutePath)
        {
            /*
            IStaticDataService staticDataService = new StaticDataService();
            IProductService productService = new ProductService();
            ProductDO prod = productService.GetProductById(productId);
            //595x842
            Document doc = new Document(PageSize.A4);

            PdfWriter writer = PdfWriter.GetInstance(doc, new FileStream(absolutePath, FileMode.OpenOrCreate));
            doc.Open();

            AddPdfHeaders(doc);
            
            PdfContentByte cb = writer.DirectContent;
            //DrawCutRectangle(cb);
            DrawRegistrationMarks(cb);
            
            /// Adds background images to pdf
            for (int row = 0; row < 5; row++)
            {
                for (int col = 0; col < 2; col++)
                {
                    iTextSharp.text.Image img = iTextSharp.text.Image.GetInstance(FileStoreService.GetPrintFilePath(prod.TypeKey,prod.Id));
                    img.ScaleToFit(NATIONAL_BC_WIDTH, NATIONAL_BC_HEIGHT);
                    img.SetAbsolutePosition(A4_MARGIN_SIDES + col * NATIONAL_BC_WIDTH, A4_MARGIN_TOPBOT + row * NATIONAL_BC_HEIGHT);
                    doc.Add(img);
                }
            }

            /// Adds texts to pdf
            cb.BeginText();
            foreach (TextItem item in infoData.Items)
            {
                //float textHeight = item.Size / 0.615f;
                float x = 0f;

                if (item.Align.Halign.Equals(HalignType.CENTER))
                {
                    x = (NATIONAL_BC_WIDTH - CUT_EXT_LENGTH) / 2F;
                }
                else
                {
                    x = item.Align.X;
                }
                    
                //else if (item.Align.Halign.Equals(HalignType.ABSOLUTE))
               // {
                //    x = (item.Align.X / NATIONAL_BC_VIEW_WIDTH) * (NATIONAL_BC_WIDTH - CUT_EXT_LENGTH);
                //}

                float y = item.Align.Y;// (1 - (item.Align.Y + textHeight) / NATIONAL_BC_VIEW_HEIGHT) * (NATIONAL_BC_HEIGHT - CUT_EXT_LENGTH);

                int alignType = 0;
                if (item.Align.GrowTo.Equals(GrowToType.MIDDLE))
                {
                    alignType = PdfContentByte.ALIGN_CENTER;
                }
                else if (item.Align.GrowTo.Equals(GrowToType.LEFT))
                {
                    alignType = PdfContentByte.ALIGN_RIGHT;
                }
                else if (item.Align.GrowTo.Equals(GrowToType.RIGHT))
                {
                    alignType = PdfContentByte.ALIGN_LEFT;
                }

                for (int row = 0; row < 5; row++)
                {
                    for (int col = 0; col < 2; col++)
                    {
                        float pdfX = x + A4_MARGIN_SIDES + CUT_EXT_LENGTH / 2 + col * NATIONAL_BC_WIDTH;
                        float pdfY = y + A4_MARGIN_TOPBOT + CUT_EXT_LENGTH / 2 + row * NATIONAL_BC_HEIGHT;
                        pdfX = (float)Math.Round(pdfX, 2);
                        pdfY = (float)Math.Round(pdfY, 2);
                        
                        //int fontSize = item.Size;
                        BaseFont font = GetBaseFont(staticDataService.GetFontById(item.FontId));

                        var styledFont = new iTextSharp.text.Font(font, item.Size, iTextSharp.text.Font.ITALIC);
                        cb.SetFontAndSize(styledFont.GetCalculatedBaseFont(false), item.Size);
                        
                        
                        //Color color = Color.FromArgb(Convert.ToInt32(item.Color, 16));
                        //cb.SetRGBColorFill(color.R, color.G, color.B);
                        cb.SetCMYKColorFill(item.CMYK.C, item.CMYK.M, item.CMYK.Y, item.CMYK.K);
                        cb.ShowTextAligned(alignType, item.Text, pdfX, pdfY, 0);
                    }
                }
            }
            cb.EndText();
            
            doc.Close();
            writer.Close();*/
        }
        /*
        private BaseFont GetBaseFont(FontDO fontDo)
        {
            
            //Be carefull with encoding use IDENTITY_H for unicode
            return BaseFont.CreateFont(FileStoreService.GetFontFilePath(fontDo.Name, fontDo.FileName), 
                                BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
        }

        private void AddPdfHeaders(Document doc)
        {
            doc.AddAuthor("Simply LLC");
            doc.AddTitle("Business Card template");
        }*/
    }
}
